<?php

namespace App\Http\Controllers\admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//模型
use App\Models\Auth;
use App\Models\Icon;
use App\Models\Role;

class AuthController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $info = Auth::paginate(10);
        $data = $info -> all();
        foreach ($data as $key=>$val){
            $data[$key]['icon'] = Auth::find($val['id']) -> icon() -> first() -> icon;
        }
        return view('admin.auth.index',['data'=>$data,'info'=>$info,'request'=>$request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //查询图标
        $iconInfo = Icon::all();
        //模块
        $modInfo = Auth::where('level',1) -> select('id','module') -> get();
        return view('admin.auth.create',['modInfo'=>$modInfo,'iconInfo'=>$iconInfo]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        if($info['auth_id'] == 0){
            $info['level'] = 1;
        }else{
            $info['module'] = Auth::find($info['auth_id']) -> module;
            $info['level'] = 2;
        }
        //添加区域表
        $res = Auth::create($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Auth::find($id) -> toarray();
        //查询图标
        $iconInfo = Icon::all();
        //模块
        $modInfo = Auth::where('level',1) -> select('id','module') -> get();
        return view('admin.auth.edit',['data'=>$data,'modInfo'=>$modInfo,'iconInfo'=>$iconInfo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        if($info['auth_id'] == 0){
            $info['level'] = 1;
            //查询权限表
            Auth::where('module',Auth::find($id) -> module) -> update(['module'=>$info['module']]);
            $res = Auth::find($id) -> update($info);
        }else{
            $info['module'] = Auth::find($info['auth_id']) -> module;
            $info['level'] = 2;
            $res = Auth::find($id) -> update($info);
        }
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $condition = false;
        $id = $request -> input('id');
        //查询权限表
        if(Auth::find($id) -> level == 1){
            $num = Auth::where(['module'=>Auth::find($id) -> module,'level'=>2]) -> count();
            if($num == 0){
               $condition = true;
            }
        }else{
            $condition = true;
        }
        if($condition){
            //查询角色下是否有该权限,有不能删除
            $count = Role::where(DB::raw("concat(',',auth_ids,',')"),'like',"%,".$id.",%") -> count();
            if($count == 0){
                //查询区域和客户的关系表并删除
                $res = Auth::destroy($id);
                if($res){
                    $data['status'] = 1;
                }else{
                    $data['status'] = -1;
                }
            }else{
                $data['status'] = -1;
            }
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }
}
